// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
 * RelativeFileProtocol.cpp
 *
 *  Created on: Feb 4, 2009
 *      Author: nakada
 */

#include <unistd.h>
#include <sys/param.h>
#include "RelativeFileProtocol.hh"
#include "URI.hpp"
#include <iostream>
#include <boost/foreach.hpp>


RelativeFileProtocol::RelativeFileProtocol(){
  addName("rfile");
}

RelativeFileProtocol::RelativeFileProtocol::RelativeFileProtocol(const RelativeFileProtocol& protocol):
  Protocol(protocol){}
RelativeFileProtocol::~RelativeFileProtocol(){}



/**
 *  replace "rfile" with "file" , appending CWD to path
 */
static std::string replaceURI(const std::string & sourceURI){
  ninf::lib::URI uri(sourceURI);
  char buf[MAXPATHLEN];
  getcwd(buf, MAXPATHLEN);
  std::string tmp = "file://" + std::string(buf) + "/" + uri.path;

  std::cout << tmp << std::endl;

  return tmp;
}

static std::list<std::string> replaceURI(std::list<std::string> sourceURIs){
  std::list<std::string> tmp;
  BOOST_FOREACH(std::string uri, sourceURIs) {
    tmp.push_back(replaceURI(uri));
  }
  return tmp;
}


void RelativeFileProtocol::downloadDataFile(const std::string& sourceURI,
                                const Credential& credential=Credential()) {
  httpProtocol.downloadDataFile(replaceURI(sourceURI), credential);
}

void RelativeFileProtocol::downloadDataFile(const std::string& sourceURI,
                                const std::string& path,
                                const Credential& credential=Credential()) {
  httpProtocol.downloadDataFile(replaceURI(sourceURI), path, credential);
}

void RelativeFileProtocol::downloadDataFile(const std::list<std::string>& sourceURIs,
                                const std::list<Credential>& credentials){
  httpProtocol.downloadDataFile(replaceURI(sourceURIs), credentials);
}

void RelativeFileProtocol::downloadDataFile(const std::list<std::string>& sourceURIs,
                                const std::string& path,
                                const std::list<Credential>& credentials){
  httpProtocol.downloadDataFile(replaceURI(sourceURIs), path, credentials);
}

void RelativeFileProtocol::downloadDataMemory(const std::string& sourceURI,
                                  void* const dest,
                                  const Credential& credential=Credential()){
  httpProtocol.downloadDataMemory(replaceURI(sourceURI), dest, credential);
}

void RelativeFileProtocol::downloadDataMemory(const std::string& sourceURI,
                                  void** dest,
                                  size_t* size,
                                  const Credential& credential=Credential()){
  httpProtocol.downloadDataMemory(replaceURI(sourceURI), dest, size, credential);
}

void RelativeFileProtocol::downloadDataMemory(const std::list<std::string>& sourceURIs,
                                  void* const dest,
                                  const std::list<Credential>& credentials){
  httpProtocol.downloadDataMemory(replaceURI(sourceURIs), dest, credentials);
}

void RelativeFileProtocol::downloadDataMemory(const std::list<std::string>& sourceURIs,
                                  void** dest,
                                  size_t* size,
                                  const std::list<Credential>& credentials){
  httpProtocol.downloadDataMemory(replaceURI(sourceURIs), dest, size, credentials);
}

void RelativeFileProtocol::uploadDataFile(const std::string& destinationURI,
                              const std::string& path,
                              const Credential& credential=Credential()){
  httpProtocol.uploadDataFile(replaceURI(destinationURI), path, credential);
}

void RelativeFileProtocol::uploadDataMemory(const std::string& destinationURI,
                                void* const src,
                                size_t      src_size,
                                const Credential& credential=Credential()){
  httpProtocol.uploadDataMemory(replaceURI(destinationURI), src, src_size, credential);
}

